package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户成功登陆后,所显示的界面
 */
@Controller
public class IndexController {
    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ITravelCommendService travelCommendService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationTemplateSearchService destinationTemplateSearchService;
    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;
    @Reference
    private ITravelTemplateSearchService travelTemplateSearchService;
    @Reference
    private IUserInfoTemplateSearchService userInfoTemplateSearchService;
    @Reference
    private ISearchService searchService;

    @RequestMapping("")
    public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
        /*String token = CookieUtil.getToken(request, response);
        UserInfo userInfo = userInfoRedisService.getUserInfo(token);
        //共享userInfo对象  model只能在一次请求中进行共享数据
        //所以使用session  会话作用域    只有一个浏览器
        request.getSession().setAttribute("userInfo", userInfo);*/
        //tcs   首页的五跳数据 推荐游记
        model.addAttribute("tcs", travelCommendService.getTop5());
        //sc    攻略的推荐
        model.addAttribute("sc", strategyCommendService.getTop5().get(0));
        return "index/index";
    }

    //游记的显示 currentPage: 1  orderType: 2
    @RequestMapping("/index/travelPage")
    public String travelPage(TravelQuery qo, Model model) {
        //pageInfo
        model.addAttribute("pageInfo", travelService.query(qo));
        return "index/travelPageTpl";
    }

    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo") SearchQueryObject qo) {
        switch (qo.getType()) {
            case SearchQueryObject.SEARCH_TYPE_DEST:
                return searchDest(qo, model);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return searchStrategy(qo, model);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return searchTravel(qo, model);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return searchUser(qo, model);
            default:
                return searchAll(qo, model);
        }
    }

    private String searchDest(SearchQueryObject qo, Model model) {
        //目的地搜索
        //判断 该keyword是否存在
        DestinationTemplate dest = destinationTemplateSearchService.findByName(qo.getKeyword());
        if (dest != null) {
            SearchResultVO vo = new SearchResultVO();
            //目的地存在的情况下 , 获取目的地能关联的东西
            List<StrategyTemplate> ss = strategyTemplateSearchService.findByDestName(qo.getKeyword());
            vo.setTotal(vo.getTotal() + ss.size());
            //截取4条 , 防止数据太大
            if (ss.size() > 4) {
                ss = ss.subList(0, 4);
            }
            List<TravelTemplate> ts = travelTemplateSearchService.findByDestName(qo.getKeyword());
            vo.setTotal(vo.getTotal() + ts.size());
            if (ts.size() > 4) {
                ts = ts.subList(0, 4);
            }
            List<UserInfoTemplate> us = userInfoTemplateSearchService.findByDestName(qo.getKeyword());
            vo.setTotal(vo.getTotal() + us.size());
            if (us.size() > 4) {
                us = us.subList(0, 4);
            }
            //页面需要data  中有dests , travels , users
            vo.setStrategys(ss);
            vo.setTravels(ts);
            vo.setUsers(us);
            model.addAttribute("data", vo);

        }
        model.addAttribute("dest", dest);
        return "index/searchDest";
    }

    private String searchStrategy(SearchQueryObject qo, Model model) {
        //攻略搜索
        model.addAttribute("page", searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary"));
        return "index/searchStrategy";
    }

    private String searchTravel(SearchQueryObject qo, Model model) {
        //游记搜索
        model.addAttribute("page", searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary"));
        return "index/searchTravel";
    }

    private String searchUser(SearchQueryObject qo, Model model) {
        //用户搜索  page
        model.addAttribute("page", searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName"));
        return "index/searchUser";
    }

    private String searchAll(SearchQueryObject qo, Model model) {
        //所有搜索  data
        SearchResultVO vo = new SearchResultVO();
        Page<StrategyTemplate> ss = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class,
                qo, "title", "subTitle", "summary");
        Page<TravelTemplate> ts = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class,
                qo, "title", "summary");
        Page<UserInfoTemplate> us = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class,
                qo, "nickname", "destName");
        Page<DestinationTemplate> ds = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME, DestinationTemplate.class,
                qo, "name", "info");
        vo.setUsers(us.getContent());
        vo.setTravels(ts.getContent());
        vo.setStrategys(ss.getContent());
        vo.setDests(ds.getContent());
        vo.setTotal(ss.getTotalElements() + ts.getTotalElements() + us.getTotalElements() + ds.getTotalElements());
        model.addAttribute("data", vo);
        return "index/searchAll";
    }
}
